package com.rlz.framemm.ui;

import android.graphics.Color;
import android.view.View;

import com.rlz.base.base.BaseActivity;
import com.rlz.base.tools.adapter.BaseAdapter;
import com.rlz.base.tools.refresh.PullListener;
import com.rlz.base.tools.refresh.RefreshLayout;
import com.rlz.base.tools.rxbus.RxBus;
import com.rlz.base.util.DataHelper;
import com.rlz.base.util.Paging;
import com.rlz.base.util.StatusBarHelper;
import com.rlz.base.weiget.StateView;
import com.rlz.framemm.R;
import com.rlz.framemm.bean.ArticleBean;
import com.rlz.framemm.databinding.ActivityListBinding;
import com.rlz.framemm.model.ListModel;
import com.rlz.framemm.ui.adapter.ArticleAdapter;
import com.rlz.framemm.viewmodel.ListViewModel;
import com.rlz.net.NetParams;

import java.util.ArrayList;

import androidx.recyclerview.widget.LinearLayoutManager;

import static com.rlz.base.base.BaseWebActivity.URL_CARRY_STR;


/**
 * Created by RLZ
 * on 2021/4/8
 */
public class ArticleActivity extends BaseActivity<ListModel, ListViewModel, ActivityListBinding> implements BaseAdapter.OnItemClickListener<ArticleBean.ListBean>, PullListener {

    private ArticleAdapter mAdapter;
    private final ArrayList<ArticleBean.ListBean> mList = new ArrayList<>();


    @Override
    public int layoutId() {
        return R.layout.activity_list;
    }

    @Override
    public void logic() {

        StatusBarHelper.setStatusBarColor(this, Color.WHITE);
        StatusBarHelper.setDarkMode(this);
        mPaging = new Paging();
        mBinding.RecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ArticleAdapter();
        mBinding.RecyclerView.setAdapter(mAdapter);
        mAdapter.addData(mList);
        getArticleList();
        RxBus.Companion.get().post(11,"这个发出去了呢");
    }

    @Override
    protected void click() {
        super.click();
        mBinding.RefreshLayout.setOnRefreshListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    private void getArticleList() {
        mViewModel.getArticleList(mPaging.curPage,
                NetParams.build().dialog(true).loading("数据加载中哦哦")).observe(this, resource -> {
            resource.handler(new OnCallback<ArticleBean>() {
                @Override
                public void onSuccess(ArticleBean data) {
                    if (data.getDatas() == null || data.getDatas().size() < 1) {
                        showStateView(StateView.EMPTY);
                        return;
                    }
                    showStateView(StateView.SUCCESS);
                    DataHelper.setData(mPaging.curPage, mList, data.getDatas(), mAdapter, mBinding.RefreshLayout);
                }
            });
        });
    }


    @Override
    protected void retryClick(View view) {
        super.retryClick(view);
        getArticleList();
    }

    @Override
    public void onItemClick(ArticleBean.ListBean item, int position) {
        mBundle.putString(URL_CARRY_STR, item.getLink());
        to(Web1Activity.class);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        assert mPaging != null;
        mPaging.curPage = 0;
        getArticleList();
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        assert mPaging != null;
        mPaging.curPage++;
        getArticleList();
    }
}
